#coding=utf-8
from config import *
from tools.mini_tool import get_action_status
import random

# 根据玩家位置，更新敌对势力的位置
def update_HostileForces_image(HostileForces, player,Screen_size,passed_time):
    '''
    :param HostileForces: 敌对势力对象
    :param player_image:  玩家对象
    :param screen: 屏幕对象
    :param passed_time: 上一帧到现在的时间
    '''
    # 计算冷却时间
    if HostileForces.jn_surplus_cd_time > 0:
        HostileForces.jn_surplus_cd_time -= passed_time
    else:
        HostileForces.jn_surplus_cd_time = 0

    if HostileForces.GJ_status == True: #为攻击状态时
        distance = passed_time * (HostileForces.move_rate*2.2 / 1000)  # 算出移动距离
        HostileForces.move_time += passed_time  # 更新移动时间
        # 移动
        HostileForces.move(HostileForces.action_status, distance, Screen_size)
        # 获得当前action_step_number（动作步骤编号）
        action_step_number = int((HostileForces.move_time // HostileForces.sprite_change_rate)) % len(
            HostileForces.action_img_resource_dict[HostileForces.action_status])
        # 更新敌对人物显示精灵surface
        HostileForces.image = HostileForces.get_action_surface(HostileForces.action_status, action_step_number)
        # 当攻击动作编号为最后一个的时候，攻击状态结束
        if action_step_number > len(HostileForces.action_img_resource_dict[HostileForces.action_status]) - 2:
            HostileForces.GJ_status = False
        return 0


    old_action_status = HostileForces.action_status # 记录之前的状态
    hostile_cx,hostile_cy = HostileForces.get_center_coordinate() # 获取敌对势力的中心坐标
    player_cx,player_cy = player.get_center_coordinate() # 获取敌对势力的中心坐标
    # 判断玩家当前位置，调整敌对势力的面向、移动方向
    # 当移动模式为0时，才随机移动模式
    HostileForces.move_mode = (HostileForces.move_time // random.choice([1800,2000,2500])) % 3
    # 提供2种攻击方式
    # 当与玩家的中心点y坐标相差小于10时且冷却时间小于等于0，才能释放，触发技能攻击，猛冲
    if abs(hostile_cy - player_cy) < 10 and (hostile_cx - player_cx) < 500 and HostileForces.jn_surplus_cd_time <= 0:
        HostileForces.jn_surplus_cd_time = HostileForces.jn_cd_time # 冷却时间开始计时
        HostileForces.GJ_status = True  # 开启攻击状态，移动速率加快
        if hostile_cx > player_cx:
            HostileForces.action_status = LEFT_JN # 行动状态为发动左技能
        else:
            HostileForces.action_status = RIGHT_JN # 行动状态为发动右技能
    # 提供3种移动方式
    elif HostileForces.move_mode == 0: # one 45度角行走接近
        if HostileForces.x > player.x:
            if HostileForces.y > player.y:
                HostileForces.action_status = LEFT_UP
            elif HostileForces.y < player.y:
                HostileForces.action_status = LEFT_DOWN
        elif HostileForces.x < player.x:
            if HostileForces.y > player.y:
                HostileForces.action_status = RIGHT_UP
            elif HostileForces.y < player.y:
                HostileForces.action_status = RIGHT_DOWN

    elif HostileForces.move_mode == 1: # two 优先横向接近，次竖向
        if HostileForces.x > player.x:
            HostileForces.action_status = LEFT
        elif HostileForces.x < player.x:
            HostileForces.action_status = RIGHT
        elif HostileForces.y > player.y:
            HostileForces.action_status = UP
        elif HostileForces.y < player.y:
            HostileForces.action_status = DOWN

    elif HostileForces.move_mode == 2: # there 优先竖向接近，次横向
        if HostileForces.y > player.y:
            HostileForces.action_status = UP
        elif HostileForces.y < player.y:
            HostileForces.action_status = DOWN
        elif HostileForces.x > player.x:
            HostileForces.action_status = LEFT
        elif HostileForces.x < player.x:
            HostileForces.action_status = RIGHT

    distance = passed_time * (HostileForces.move_rate / 1000) # 算出移动距离
    HostileForces.move_time += passed_time #更新移动时间
    # 移动
    HostileForces.move(HostileForces.action_status, distance, Screen_size)
    # 移动方向，含左统一为左，含右统一为右，因为没有45度和上下的精灵资源
    if HostileForces.action_status in [LEFT_UP,LEFT_DOWN]:
        HostileForces.action_status = LEFT
    elif HostileForces.action_status in [RIGHT_UP,RIGHT_DOWN]:
        HostileForces.action_status = RIGHT
    elif HostileForces.action_status in [UP,DOWN]:
        HostileForces.action_status = old_action_status

    # 获得当前action_step_number（动作步骤编号）
    action_step_number = int((HostileForces.move_time // HostileForces.sprite_change_rate)) % len(HostileForces.action_img_resource_dict[HostileForces.action_status])

    # 更新敌对人物显示精灵surface
    HostileForces.image = HostileForces.get_action_surface(HostileForces.action_status,action_step_number)

